package com.woniuxy.classdeans.mqresceiver;

import com.rabbitmq.client.Channel;
import com.woniuxy.classdeans.service.DropoutInfoService;
import com.woniuxy.classdeans.service.GradeDeanService;
import com.woniuxy.classdeans.service.StudentInfoService;
import com.woniuxy.classdeans.service.UserService;
import com.woniuxy.classdeans.utils.WebSocketUtil;
import com.woniuxy.commons.entity.StudentInfo;
import com.woniuxy.commons.entity.User;
import com.woniuxy.commons.entity.wyh.GradeDean;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.io.IOException;
import java.util.Date;

/**
 * 监听班主任模块的退学信息
 *
 * @author 王艳辉
 * @date 2020/7/28 - 20:23
 */
@Component
@Slf4j
@Transactional
public class DropoutReceiver {

    @Autowired
    private DropoutInfoService dropoutInfoService;
    //注入学生service
    @Autowired
    private StudentInfoService studentInfoService;

    //主任年级主任service
    @Autowired
    private GradeDeanService gradeDeanService;
    //用户信息
    @Autowired
    private UserService userService;
    /**
     * 监听高三年级学生退学信息
     *
     * @param message 接受的消息
     * @param channel 接受消息的信道
     */
    @RabbitListener(queues = "routing_a")
    public void receiverGrade1(int mes, Message message, Channel channel) throws IOException {
        log.info(new Date()+"处理的消息为：" + mes);//学生id
        try {
            dropoutInfoService.addInfo(mes,new Date());//待处理学生信息存入到数据库

            //发送给对应的年级主任，如果在线弹窗提示
            sendMesToGradeDean(mes);
            //开启接受消息确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }catch (Exception e){
            //确认失败，消息重回队列
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,false);
            log.info(new Date()+"处理退学消息发送异常"+e.getMessage());
            throw new RuntimeException("处理退学信息失败");
        }
    }

    /**
     * 监听高二年级学生退学信息
     *
     * @param message 接受的消息
     * @param channel 接受消息的信道
     */
    @RabbitListener(queues = "routing_b")
    public void receiverGrade2(int mes, Message message, Channel channel) throws IOException {
        log.info(new Date()+"处理的消息为：" + mes);//学生id
        try {
            dropoutInfoService.addInfo(mes,new Date());//待处理学生信息存入到数据库

            //发送给对应的年级主任，如果在线弹窗提示
            sendMesToGradeDean(mes);
            //开启接受消息确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }catch (Exception e){
            //确认失败，消息重回队列
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,false);
            log.info(new Date()+"处理退学消息发送异常"+e.getMessage());
            throw new RuntimeException("处理退学信息失败");
        }
    }

    /**
     * 监听高三年级学生退学信息
     *
     * @param message 接受的消息
     * @param channel 接受消息的信道
     */
    @RabbitListener(queues = "routing_c")
    public void receiverGrade3(int mes, Message message, Channel channel) throws IOException {
        log.info(new Date()+"处理的消息为：" + mes);//学生id
        try {
            dropoutInfoService.addInfo(mes,new Date());//待处理学生信息存入到数据库

            //发送给对应的年级主任，如果在线弹窗提示
            sendMesToGradeDean(mes);
            //开启接受消息确认
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }catch (Exception e){
            //确认失败，消息重回队列
            channel.basicNack(message.getMessageProperties().getDeliveryTag(), false,false);
            log.info(new Date()+"处理退学消息发送异常"+e.getMessage());
            throw new RuntimeException("处理退学信息失败");
        }
    }


    //发送websocket消息提示对应的年级主任
    private void sendMesToGradeDean(int mes) {
        //获取学校信息
        StudentInfo student = studentInfoService.getStudentInfoById(mes);

        //获取学生所在年级年级主任信息
        Integer gdId = student.getTeamInfo().getGradeInfo().getGdId();

        //根据年级主任id获取年级主任信息
        GradeDean gradeDean = gradeDeanService.getByTcid(gdId);
        //根据年级主任获取他的教师id
        Integer tcId = gradeDean.getTcId();
        //根据教师id获取user表的老师账号uname;

        User teacher = userService.getByRidAndRole("teacher", tcId);
        String uname=teacher.getAccount();

        //调用websocket给该对象发送消息，如果存在的话就发送消息提示
        WebSocketUtil.sendToTokenMessage("18371458685","您有新的待处理退学信了！");
    }
}
